package com.rongkang.interview.juc.multithreading;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description:
 * @author: 荣康
 * @date: 2021/12/13
 * <p>
 * 使用线程池获取线程
 * Executors创建线程一个都不用，阿里开发手册规定了
 */
public class MyThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);//固定线程数的线程池
//        ExecutorService threadPool = Executors.newSingleThreadExecutor(); //一池一个线程
//        ExecutorService threadPool = Executors.newCachedThreadPool();//一池多个线程
        //模拟10个用户来办业务，每个用户就是一个来自外部的请求
        try {
            for (int i = 0; i < 30; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" \t 办理业务");
                });
//                try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); }
            }
        } finally {
            threadPool.shutdown();
        }
    }
}
